<?php 

/*
 * 检查用户登陆状态
 * @date: 2015-3-21上午10:55:57
 * @editor: XING
 */
if(!function_exists('is_login')){
	function is_login(){
		global $isLogin;
		$isLogin = false;
		$loginCookie = substr(cookie('ajwUid'),0,-1);
		if(isset($loginCookie) && $loginCookie){
            $loginUid = base64_decode($loginCookie);
            if(session($loginUid)){
				$isLogin = true;
			}else{
				$rememberNum = M('ResEntity')->getFieldByUsername($loginUid,'remember_token');
				if($rememberNum == cookie('_ajwunion')){
					$isLogin = true;
				}
			}
		}
		return $isLogin;
	}
}

if(!function_exists('getLoginUname')){
    function getLoginUname(){
        $uname = session('loginUname');
        if($uname){
            return $uname;
        }
        $loginCookie = substr(cookie('ajwUid'),0,-1);
        if(isset($loginCookie) && $loginCookie){
            return base64_decode($loginCookie);
        }
    }
}

/*
 * 获取登陆用户的登陆信息
 * @date: 2015-3-21上午11:00:13
 * @editor: XING
 */
if(!function_exists('getLoginMsg')){
	function getLoginMsg(){
		global $isLogin;
		$loginMsgObj = new stdClass();
		$loginMsgObj->isLogin = $isLogin;
		$loginMsgObj->username = base64_decode(substr(cookie('ajwUid'),0,-1));
		$users = M('ResEntity')->where(array('username'=>$loginMsgObj->username))->find();
		$loginMsgObj->rid = $users['rid'];
		$loginMsgObj->email = $users['email'];
		
		return $loginMsgObj;
	}
}

/*
 * 返回随机字符串
 * @date: 2015-3-21下午3:34:47
 * @editor: XING
 */
if(!function_exists('randstr')){
	function randstr($num=1){
		$return = '';
		$randstr = '1023456789abcdefghijklmnopqrstuvwxyz';
		for($i=0;$i<$num;$i++){
			$return .= $randstr{rand(0,35)};
		}
		
		return $return;
	}
}


/*
 * 日志添加记录
 * @date: 2015-4-1下午5:54:56
 * @editor: XING
 */
if(!function_exists('putlog')){
	function putlog($msg,$controller,$fun){
		M('SysLogs')->add(array(
			'sys_controller_name' => $controller,
			'sys_function_name' => $fun,
			'sys_remark' => (I('post.username') ? I('post.username') : '').$msg,
			'sys_editor' => getLoginMsg()->rid,
			'add_at' => time(),
			'update_at' => time()
		));
	}
}

/*
 * JSON格式化输出
 * @date: 2015-12-13 下午5:09:55
 * @editor: XING
 */
if(!function_exists('dataFormat')){
	function dataFormat($data=array(),$recode=1,$msg='',$adds=array()){
		if($data || $adds){
			echo json_encode(array_merge(array(
					'data' => $data,
					'code' => $recode,
					'msg' => $msg ? $msg : 'success'
			),$adds));exit;
		}else{
			echo json_encode(array_merge(array(
					'code' => $recode,
					'msg' => $msg ? $msg : '没有相关数据'
			)));exit;
		}
	}
}

/*
 * 订单生成码
 * @date: 2015-6-4 下午4:51:01
 * @editor: XING
 */
if(!function_exists('mkorder')){
	function mkorder(){
		$creatOid = date("Y").substr(time(),4).rand(10000,99999);
		$isExists = M('OrderEntity')->where(array('order_id'=>$creatOid))->count();
		if($isExists > 0)
			return mkorder();
		else
			return $creatOid;
	}
}

if(!function_exists('uploadQiniu')){
	function uploadQiniu($img,$name=''){
	    $filePath = $img;
	    $explode = explode('.', $filePath);
	    $type = $explode[count($explode)-1];
	    $key = $name.'_'.time().'_'.rand(1000,9999).'.'.$type;
	    // 要上传文件的本地路径
	    $data = (new \Org\Util\Qiniu())->upFile($filePath,$key);
	    $qiniu_file_url = QINIUHOST.$data['key'];
	    if($qiniu_file_url){
	        unlink($filePath);
	    }
	    return $qiniu_file_url;
	}	
}

if(!function_exists('thumbImg')){
    function thumbImg($img_path, $ratio, $is_del = true){
        $image = new \Think\Image();
        $image->open($img_path);
        $width = $image->width(); // 返回图片的宽度
        $height = $image->height();
        $thumb_w = $width * $ratio; //取得图片的长宽比
        $thumb_h = $height * $ratio;
        //如果文件路径不存在则创建
        $save_path_info = pathinfo($img_path);
        $new_name = date("YmdHis") . '_' . rand(10000, 99999);
        if(!is_dir($save_path_info['dirname'])) mkdir ($save_path_info['dirname'], 0777);
        $save_path = $save_path_info['dirname'].'/'.$new_name.'.'.$save_path_info['extension'];
        $image->thumb($thumb_w, $thumb_h)->save($save_path);
        if($is_del) @unlink($img_path); //删除源文件
        return $save_path;
    }	
}

if(!function_exists('cut_str')){
    function cut_str($str,$len,$suffix="..."){
        if(function_exists('mb_substr')){
            if(strlen($str) > $len){
                $str= mb_substr($str,0,$len,'utf8').$suffix;
            }
            return $str;
        }else{
            if(strlen($str) > $len){
                $str= substr($str,0,$len).$suffix;
            }
            return $str;
        }
    }
}

/*
 * 返回还入仓库名称(传参仓库id)
 * @date: 2017-12-7 下午4:51:01
 * @editor: zyy
 */
if(!function_exists('storageName')){
    function storageName($id){
        if($id){
            $sname = M('warehouse')->where(['warehouse_id'=>$id])->getField('warehouse_name');
            return $sname;
        }
    }
}

?>